package com.hyfd.service.mp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.hyfd.common.BaseJson;
import com.hyfd.common.GlobalSetHyfd;
import com.hyfd.common.Page;
import com.hyfd.dao.mp.ProviderBillGroupDao;
import com.hyfd.dao.mp.ProviderDao;
import com.hyfd.dao.mp.ProviderGroupBillRelDao;
import com.hyfd.service.BaseService;

@Service
@Transactional
public class ProviderGroupBillRelSer extends BaseService{
	
	public Logger log = Logger.getLogger(this.getClass());
	
	@Autowired
	ProviderBillGroupDao providerBillGroupDao;
	@Autowired
	ProviderDao provider;
	@Autowired
	ProviderGroupBillRelDao providerGroupBillRelDao;
	
	public String providerGroupBillRelEditPage(String id){
		try{
			Map<String, Object> providerBillGroup=providerBillGroupDao.selectByPrimaryKey(id);
			Session session=getSession();
			session.setAttribute("providerBillGroup", providerBillGroup);
		}catch(Exception e){
			getMyLog(e,log);
		}
		return "mp/providerGroupBillRelEdit";
	}
	
	/**
	 * 根据条件分页获取话费通道组通道列表并生成json
	 * @param req
	 * @return
	 */
	public String providerGroupBillRelList(String groupId,HttpServletRequest req){
		StringBuilder sb=new StringBuilder();
		try{
		Map<String, Object> m=getMaps(req); //封装前台参数为map
		if(groupId!=null&&groupId!=""&&!groupId.equals("undefined")){
			String providerName = (String) m.get("providerName");
			String providerId = provider.getIdByName(providerName);
			if(providerId!=null){
				m.put("providerId", providerId);
			}
			m.put("groupId", groupId);
			Page p=getPage(m);//提取分页参数
			int total=getCountCount(m);
			p.setCount(total);
			int pageNum=p.getCurrentPage();
			int pageSize=p.getPageSize();
			
			sb.append("{");
			sb.append(""+getKey("page")+":"+p.getCurrentPage()+",");
			sb.append(""+getKey("total")+":"+p.getNumCount()+",");
			sb.append(""+getKey("records")+":"+p.getCount()+",");
			sb.append(""+getKey("rows")+":"+"");
			
			PageHelper.startPage(pageNum, pageSize);//mybatis分页插件
			List<Map<String, Object>> billList=providerGroupBillRelDao.selectAll(m);
			List<Map<String, Object>> List = new ArrayList<Map<String,Object>>();
			for(int i = 0 ; i<billList.size();i++){
				Map<String, Object> map = billList.get(i);
				String pName = (String) map.get("providerName");
				String pCode = (String) map.get("province_code");
				String channelName = pName+"("+pCode+")";
				map.put("channelName", channelName);
				List.add(map);
			}
			String dataListJson=BaseJson.listToJson(List);
			sb.append(dataListJson);
			sb.append("}");
		}
		
		}catch(Exception e){
			getMyLog(e,log);
		}
		
		return sb.toString();
	}
	
	/**
	 * 获取记录数量
	 * @param m
	 * @return
	 */
	public int getCountCount(Map<String, Object> m){
		int providerGroupBillRelCount=0;
		try{
			providerGroupBillRelCount=providerGroupBillRelDao.selectCount(m);
		}catch(Exception e){
			getMyLog(e,log);
		}
		return providerGroupBillRelCount;
	}
	
	public String providerGroupBillRelEdit(HttpServletRequest req){
		Map<String, Object> myData=getMaps(req);
		String groupId = (String) myData.get("groupId");
		String providerId = (String) myData.get("providerId");
		try{
			boolean flag=false;
			Map<String, Object> userInfoMap=getUser(); //取到当前用户信息
			String name = (String) userInfoMap.get("suName");
			if(groupId!=null&&groupId!=""&&providerId!=null&&providerId!=""){
				List<Map<String, Object>> list = providerGroupBillRelDao.selectAll(myData);
				if(list.size()>0){
					 boolean i = providerGroupBillRelDao.deleteBygroupIdAndProviderId(myData);
					 if(i){
						 myData.put("name", name);
						 flag = providerGroupBillRelAdd(myData);
					 }
				}else{
					flag = providerGroupBillRelAdd(myData);
				 }
			}
			Session session=getSession();
			session.setAttribute(GlobalSetHyfd.backMsg, flag?"更新成功":"更新失败");
		}catch(Exception e){
			getMyLog(e,log);
		}
		return "";	
	}
	
	public boolean providerGroupBillRelAdd(Map<String,Object> m){
		boolean flag=false;
		try {
			String groupId = (String) m.get("groupId");
			String providerId = (String) m.get("providerId");
			String createUser = (String) m.get("name");
			m.remove("groupId");
			m.remove("providerId");
			m.remove("name");
			Map<String,Object> map = new HashMap<String, Object>();
			for (Map.Entry<String, Object> entry : m.entrySet()) {
				String provinceCode = entry.getKey();
				String cityCode = (String) entry.getValue();
				if(cityCode!=null&&cityCode!=""){
					map.put("cityCode", cityCode);
				}
				map.put("groupId", groupId);
				map.put("providerId", providerId);
				map.put("provinceCode", provinceCode);
				map.put("createUser", createUser);
				int i = providerGroupBillRelDao.insertSelective(map);
			}
			flag=true;
		} catch (Exception e) {
			getMyLog(e,log);
		}
		
		return flag;
	}
    
}
